package com.warrior.bluetooth;

import android.util.Log;

import com.warrior.main.MyApp.APP_STATES;
import com.warrior.main.MyLog;

public class MyMessage{
	private int counterMessage;
	private APP_STATES state;
	private Long lData,receiveTime;
	private TYPE_DATA type;
	private String sData;
	private boolean isIncludeCallback;
	private static final String SEPARATION_DATA = "#";
	
	public MyMessage() {
		
	}
	public MyMessage(int counterMessage,TYPE_DATA type,APP_STATES state,Long data,boolean isIncludeCallback) {
		this.counterMessage = counterMessage;
		this.type = type;
		this.state = state;
		this.lData = data;
	}
	public MyMessage(int counterMessage,TYPE_DATA type,APP_STATES state,String data,boolean isIncludeCallback) {
		this.counterMessage = counterMessage;
		this.type = type;
		this.state = state;
		this.sData = data;
	}
	
	public int getCounterMessage() {
		return counterMessage;
	}
	public TYPE_DATA getType() {
		return type;
	}
	public APP_STATES getState() {
		return state;
	}
	public long getLData() {
		return lData;
	}
	public String getSData() {
		return sData;
	}
	public long getReceiveTime() {
		return receiveTime;
	}
	public boolean isIncludeCallback() {
		return isIncludeCallback;
	}
	public void setCounterMessage(int counterMessage) {
		this.counterMessage = counterMessage;
	}
	public void setType(TYPE_DATA type) {
		this.type = type;
	}
	public void setState(APP_STATES state) {
		this.state = state;
	}
	public void setData(long data) {
		this.lData = data;
	}
	public void setReceiveTime(long receiveTime) {
		this.receiveTime = receiveTime;
	}
	public void setSData(String sData) {
		this.sData = sData;
	}
	public void setIncludeCallback(boolean isIncludeCallback) {
		this.isIncludeCallback = isIncludeCallback;
	}
	public static String buildSendMessage(int counterSendMessage,int typeData,
			int orginState,Long data,boolean isIncludeCallback){
		StringBuilder message = new StringBuilder();
		message.append(counterSendMessage);
		message.append(SEPARATION_DATA);
		message.append(typeData);
		message.append(SEPARATION_DATA);
		message.append(orginState);
		message.append(SEPARATION_DATA);
		message.append(data);
		message.append(SEPARATION_DATA);
		message.append(isIncludeCallback);
		return message.toString();
	}
	public static String buildSendMessage(int counterSendMessage,int typeData,
			int orginState,String data,boolean isIncludeCallback){
		StringBuilder message = new StringBuilder();
		message.append(counterSendMessage);
		message.append(SEPARATION_DATA);
		message.append(typeData);
		message.append(SEPARATION_DATA);
		message.append(orginState);
		message.append(SEPARATION_DATA);
		message.append(data);
		message.append(SEPARATION_DATA);
		message.append(isIncludeCallback);
		return message.toString();
	}
	public static MyMessage buildReceiveMessage(String message){
		String[] arrMessage;
		MyMessage myMessage = null;
		try{
			Long lD;
			String sD;
			TYPE_DATA type;
			APP_STATES state;
			int counterReceiveMessage;
			boolean isIncludeCallback;
			
			arrMessage = message.split(SEPARATION_DATA);
			for (int i = 0; i < arrMessage.length; i++) {
				arrMessage[i].replace(SEPARATION_DATA, "");
			}
			
			counterReceiveMessage = Integer.parseInt(arrMessage[0]);
			type = BuildTYPE_DATA(Integer.parseInt(arrMessage[1]));
			state = BuildAPP_STATES(Integer.parseInt(arrMessage[2]));
			isIncludeCallback = Boolean.parseBoolean(arrMessage[4]);
			if(type == TYPE_DATA.number){
				lD = Long.parseLong(arrMessage[3]);
				myMessage = new MyMessage(counterReceiveMessage,type,state,lD,isIncludeCallback);
			}
			else{
				sD = arrMessage[3];
				myMessage = new MyMessage(counterReceiveMessage,type,state,sD,isIncludeCallback);
			}
		}
		catch(NumberFormatException e){
			Log.d(MyLog.MY_MESSAGE_EXEPTION,"NumberFormatException: we need to other device data not received" +
					"message received:" + message + " messageException: " + e.getMessage());
		}
		return myMessage;
	}
	static private TYPE_DATA BuildTYPE_DATA(int typeData){
		TYPE_DATA type;
		switch (typeData) {
			case 0:{
				type = TYPE_DATA.number;
				break;
			}
			case 1:{
				type = TYPE_DATA.word;
				break;
			}
			default:{
				type = TYPE_DATA.number;
				break;
			}
		}
		return type;
	}
	static private APP_STATES BuildAPP_STATES(int orginState){
		APP_STATES state;
		switch (orginState) {
			case 0:{
				state = APP_STATES.SYNC;
				break;
			}
			case 1:{
				state = APP_STATES.SELECT_GAME;
				break;
			}
			case 2:{
				state = APP_STATES.RUN_GAME;
				break;
			}
	
			default:{
				state = APP_STATES.SYNC;
				break;
			}
		}
		return state;
	}
	@Override
	public String toString() {
		String str;
		str = "counter:" + getCounterMessage() + ",type:" + getType() 
				+ ",state:" + getState() + ",data:";
		if(type == TYPE_DATA.number){
			str += getLData();
		}
		else{
			str += getSData();
		}
		return str;
	}
}
enum TYPE_DATA{
	number,
	word
}
